package com.jie.langlangmall.controller;

import com.jie.langlangmall.common.api.CommonPage;
import com.jie.langlangmall.common.api.CommonResult;
import com.jie.langlangmall.dto.PmsProductAttributeCategoryItem;
import com.jie.langlangmall.model.PmsProductAttributeCategory;
import com.jie.langlangmall.service.PmsProductAttributeCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/23
 * @Email 3217998214@qq.com
 * @Description 商品属性分类管理Controller
 */
@RestController
@Tag(name = "PmsProductAttributeCategoryController", description = "商品属性分类管理")
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;

    @Operation(summary = "分页查询所有商品属性分类")
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsProductAttributeCategory> productAttributeCategoryList = productAttributeCategoryService.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(productAttributeCategoryList));
    }

    @Operation(summary = "获取所有商品属性分类以及下属属性")
    @GetMapping("/list/withAttr")
    public CommonResult<List<PmsProductAttributeCategoryItem>> listWithAttr() {
        List<PmsProductAttributeCategoryItem> productAttributeCategoryList = productAttributeCategoryService.listWithAttr();
        return CommonResult.success(productAttributeCategoryList);
    }

    @Operation(summary = "修改商品属性分类")
    @PostMapping("/update/{id}")
    public CommonResult<Object> update(@PathVariable("id") Long id, @RequestParam("name") String name) {
        productAttributeCategoryService.update(id, name);
        return CommonResult.success();
    }

    @Operation(summary = "删除单个商品属性分类")
    @GetMapping("/delete/{id}")
    public CommonResult<Object> delete(@PathVariable("id") Long id) {
        productAttributeCategoryService.delete(id);
        return CommonResult.success();
    }

    @Operation(summary = "添加商品属性分类")
    @PostMapping("/create")
    public CommonResult<Object> create(@RequestParam("name") String name) {
        productAttributeCategoryService.create(name);
        return CommonResult.success();
    }

    @Operation(summary = "获取单个属性分类信息")
    @GetMapping("/{id}")
    public CommonResult<PmsProductAttributeCategory> getItem(@PathVariable("id") Long id) {
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getItem(id);
        return CommonResult.success(productAttributeCategory);
    }


}
